package com.coder520.controller;

import com.coder520.common.Const;
import com.coder520.common.ResponseCode;
import com.coder520.common.ServerResponse;
import com.coder520.pojo.ReAttend;
import com.coder520.pojo.User;
import com.coder520.service.IReAttendService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by ASUS on 2017/7/8.
 */
@Controller
@RequestMapping("/reAttend")
public class ReAttendController {

    @Autowired
    private IReAttendService iReAttendService;


    @RequestMapping
    public String toReAttend(HttpSession session,Model model){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        List<ReAttend> reAttendList = iReAttendService.listReAttend(user.getUsername());
        model.addAttribute("reAttendList",reAttendList);
        return "reAttend";

    }

    @RequestMapping("/start")
    @ResponseBody
    public ServerResponse startReAttendWorkFlow(@RequestBody ReAttend reAttend, HttpSession session) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        reAttend.setReAttendStarter(user.getUsername());
        iReAttendService.startAttendFlow(reAttend);
        return ServerResponse.createBySuccess();
    }

    @RequiresRoles("leader")
    @RequestMapping("/list")
    @ResponseBody
    public ServerResponse<List<ReAttend>> listReAttendFlow() {
//        List<Task> taskList = iReAttendService.listTask(varibles);
        String username = "laowang666";


        return iReAttendService.listTask(username);

    }

    @RequestMapping("/approve")
    @ResponseBody
    public ServerResponse approve(@RequestBody ReAttend reAttend){
        return iReAttendService.approve(reAttend);
    }


}
